package com.example.demo.server;

import com.example.demo.code.CodeClass;
import com.example.demo.dao.DaoClass;
import com.example.demo.dao.DaoClassUser;
import com.example.demo.dao.DaoTestPaper;
import com.example.demo.pojo.ClassUser;
import com.example.demo.pojo.Classed;
import com.example.demo.pojo.TestPaper;
import com.example.demo.util.DateUtil;
import com.example.demo.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class ClassServer {
    @Autowired
    DaoClass daoClass;
    @Autowired
    DaoClassUser daoClassUser;
    public long getClassedNum(String key,String userId){
        if(Objects.equals(userId, ""))
            return daoClass.countByClassNameContainsOrClassTeacherContaining(key,key);
        else
            return daoClass.getNumByUserId(key, userId);
    }
    public List<Classed> findLikeNameByIndex(int index,String s,String user){
        Pageable pageable = PageRequest.of(index, 10);
        if(Objects.equals(user, ""))
            return daoClass.findByClassNameContainingOrClassTeacherContaining(s,s,pageable).getContent();
        else
            return  daoClass.getAllByUserId(s,user,pageable).getContent();
    }
    public int saveClassUser(ClassUser classUser){
        int n=200;
        try{
            daoClassUser.save(classUser);
        }
        catch (Exception exception) {
            n=400;
        }
        return n;
    }
    public String addClassImg(MultipartFile file){
        return  FileUtil.getFilePath(file,"E:\\代码\\ASP.net Web程序设计\\web程序设大作业\\后端\\workspace\\classImg","http://localhost:8443/api/classImg/",".jpg");
    }
    public CodeClass addClassed(Classed classed){
        if(classed.getClassId().equals(""))
            classed.setClassId(DateUtil.getTimeStamp()+FileUtil.getRandomString(4));
        CodeClass codeClass=new CodeClass();
        try {
            daoClass.save(classed);
            codeClass.setCode(200);
        }
        catch (Exception e){
            codeClass.setCode(400);
        }
        return codeClass;
    }

    public CodeClass deleteClass(String id){
        CodeClass codeClass=new CodeClass();
        try{
            daoClass.deleteById(id);
            codeClass.setCode(200);
        }
        catch (Exception e){
            codeClass.setCode(400);
        }
        return codeClass;
    }
    public List<Map<String,String>>getAllClass(){
        return  daoClass.getAllClass();
    }

}
